<template>
    <div class="memberManage">
        <Row style="margin-left:-14px;">
            <Form inline :label-width="90" class="search-form">
                <Form-item label="登录名" style="margin-left:-25px;">
                    <Input type="text" v-model="searchForm.loginName" clearable placeholder="请输入" style="width: 200px"/>
                </Form-item>
                <Form-item label="姓名" style="margin-left:-32px;">
                    <Input type="text" v-model="searchForm.userName" clearable placeholder="请输入" style="width: 200px"/>
                </Form-item>
                <Form-item style="margin-left:-45px;" class="br">
                    <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                    <Button @click="handleReset">重置</Button>
                    <Button @click="back" icon="ios-undo">返回</Button>
                </Form-item>
            </Form>
        </Row>
        <Row class="operation" style="margin-left:2px;">
            <Button v-has="'action_group_batch_revise'" @click="modify" type="primary" icon="ios-create-outline">批量修改</Button>
            <Button v-has="'action_group_task_release'" @click="release" type="primary" icon="ios-create-outline">待办任务释放</Button>
            <Button v-has="'action_group_transfer_group'" @click="group" type="primary" icon="ios-create-outline">转组</Button>
            <Button v-has="'action_group_on_duty'" @click="post" type="primary" icon="ios-create-outline">上岗</Button>
            <Button v-has="'action_group_leave_post'" @click="leavePost" type="primary" icon="ios-create-outline">离岗</Button>
            <Button v-has="'action_group_on_duty_record'" @click="query" type="primary" icon="ios-search-outline">上/离岗记录</Button>
            <Button v-has="'action_group_calendar_adjustment'" @click="calendar" type="primary" icon="ios-create-outline">日历调整</Button>
            <Button v-has="'action_group_calendar_record'" @click="workCalendar" type="primary" icon="ios-create-outline">日历记录</Button>
<!--            <Button @click="power" type="primary" icon="ios-create-outline">权限调整</Button>-->
            <Button v-has="'action_group_add_member'" @click="add" type="primary" icon="md-add">新增成员</Button>
        </Row>
        <div style="margin-top:10px;">
            <rui-page :defines="defines">
                <rui-table :defineId="'memberInfo'"
                           :slots="[{key:'action',slot:'action'}]"
                           :select="true"
                           :showIndex="false"
                           @on-selection-change="showSelect"
                           @loadDatas="queryMemberList"
                           ref="memberInfoTable">
                    <template slot="action" slot-scope="{ row }">
                        <div>
                            <Button v-if="$store.getters.permissions['action_group_del']" @click="del(row)" size="small" type="error">删除</Button>
                            <Button v-if="$store.getters.permissions['action_group_revise']"  @click="update(row)" size="small" type="primary">修改</Button>
                        </div>
                    </template>
                </rui-table>
            </rui-page>
        </div>
        <Modal :title="modalTitle" v-model="memberModalVisible" :mask-closable='false' :width="500" :styles="{top: '90px'}">
            <Row>
                <Form ref="memberForm" :model="memberForm" :label-width="75" :rules="priorityFormValidate">
                    <FormItem label="权重" prop="weight">
                        <Input v-model="memberForm.weight" number/>
                    </FormItem>
                    <FormItem label="总权重" prop="totalWeight">
                        <Input v-model="memberForm.totalWeight"/>
                    </FormItem>
                    <FormItem label="抢单权重" prop="grabWeight">
                        <Input v-model="memberForm.grabWeight"/>
                    </FormItem>
                    <FormItem label="日上限值" prop="weightUpper">
                        <Input v-model="memberForm.weightUpper"/>
                    </FormItem>
                </Form>
            </Row>
            <div slot="footer">
                <Button type="text" @click="cancelUser">取消</Button>
                <Button type="primary" :loading="submitLoading" @click="submitUser">提交</Button>
            </div>
        </Modal>
        <Modal :title="changeRemarksTitle" v-model="changeRemarksModalVisible" :mask-closable='false' :width="450" :styles="{top: '90px'}">
            <Row>
                <Form ref="changeRemarksForm" :model="changeRemarksForm" style="margin: 20px" :rules="memberFormValidate">
                    <FormItem label="离岗原因" prop="changeRemarks">
                        <Input v-model="changeRemarksForm.changeRemarks" type="textarea" :rows="3" style="width: 300px;"/>
                    </FormItem>
                </Form>
            </Row>
            <div slot="footer">
                <Button type="text" @click="cancelLeavePost">取消</Button>
                <Button type="primary" :loading="submitLoading" @click="updateLeavePost">提交</Button>
            </div>
        </Modal>

        <postRecord v-show="show"  ref="child"></postRecord>
        <memberAdd  v-show="show1" ref="memberChild"></memberAdd>
        <groupTransfer v-show="show2" ref="groupTransfer"></groupTransfer>
        <rightsAdjust v-show="show3" ref="rightsAdjust"></rightsAdjust>
        <calendarChange v-show="show4" ref="calendarChange"></calendarChange>
        <calendarRecord v-show="show5" ref="calendarRecord"></calendarRecord>
    </div>
</template>

<script>
    import memberInfo from "./defines/memberManage";
    import {queryMemberList,updateMember,batchUpdateMember,updatePost,deleteMember,agencyTask} from  "@/projects/afs-core-business/api/afs-case/parameter-rules/group";
    import postRecord from "@/projects/afs-core-business/pages/case/parameter-rules/seats-manage/postRecord.vue";
    import memberAdd from "@/projects/afs-core-business/pages/case/parameter-rules/seats-manage/memberAdd.vue";
    import groupTransfer from "@/projects/afs-core-business/pages/case/parameter-rules/seats-manage/groupTransfer.vue";
    import rightsAdjust from "@/projects/afs-core-business/pages/case/parameter-rules/seats-manage/rightsAdjust.vue";
    import calendarChange from "@/projects/afs-core-business/pages/case/parameter-rules/seats-manage/personalCalendar.vue";
    import calendarRecord from "@/projects/afs-core-business/pages/case/parameter-rules/seats-manage/calendarRecord.vue";
    import {disableUser,enableUser,getAllUserData} from "@/projects/basic/api/admin/user";
    import {getUserListData} from "@/projects/basic/api/admin/user";
    export default {
        components: {
            postRecord,
            memberAdd,
            groupTransfer,
            rightsAdjust,
            calendarChange,
            calendarRecord
        },
        props: ['checkid'],
        name: "memberManage",
        data() {
            let checkValue = function(rule, value, callback) {
              if(value<=0){
                    return callback("数额大于0")
                }
                return callback();
            };
            return{
                modalTitle:"",
                submitLoading:false,
                memberModalVisible:false,
                temporary:[],
                modalType: 0,
                ids:[],
                show:false,
                show1:false,
                show2:false,
                show3:false,
                show4:false,
                show5:false,
                selectCount: 0,
                selectList:[],
                postId:"",
                searchForm:{
                    pageNumber: 1,
                    pageSize: 10,
                    groupName: "",
                    userName:"",
                    groupId:"",
                    loginName:"",
                },
                userList:[],
                memberForm: {
                    weight: 0,
                    totalWeight: "",
                    grabWeight: "",
                    weightUpper: "",
                    agent:"",
                    groupLeader:"",
                    agentGroupLeader:"",
                    groupId:"",
                    list:[],
                    changeRemarks:"",
                    userRealName:"",
                },
                changeRemarksForm: {
                    changeRemarks:"",
                },
                groupNameList:[
                    {
                        value: '质检A组',
                        label: '质检A组'
                    },
                    {
                        value: '测试组',
                        label: '测试组'
                    },
                ],
                locationData: [],
                userCollocation:{
                    postStatus:"",
                    userId:"",
                    userName:"",
                    loginName:"",
                },
                priorityFormValidate: {
                    weight: [{ required: true, validator: checkValue, trigger: 'change' }
                    ]
                },
                list:[],
                changeRemarksTitle:"",
                changeRemarksModalVisible:false,
                memberFormValidate: {
                    changeRemarks: [
                        { required: true, message: '请输入离岗原因', trigger: 'change' }
                    ],
                },
            }
        },
        methods: {
            init(){
            },
            //条件搜索
            handleSearch() {
                this.queryMemberList(1,10);
            },
            //搜索重置
            handleReset() {
                this.searchForm.groupName = "";
                this.searchForm.userName = "";
                this.searchForm.loginName = "";
                this.queryMemberList(1,10);
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            //操作组管理
            revert(){
                this.$emit("back");
            },
            //返回
            back() {
                this.searchForm.loginName = "";
                this.$emit("back");
            },
            //取消
            cancelUser(){
                this.memberModalVisible = false;
            },
            //提交
            submitUser(){
                this.$refs.memberForm.validate(valid => {
                    if(valid){
                        if(this.modalType==0){
                            this.submitLoading = true;
                            let parameter1 = this.temporary;
                            let parameter2 = JSON.stringify(this.memberForm);
                            if(parameter1==parameter2){
                                this.$Message.warning("页面数据无变化");
                                this.submitLoading = false;
                                return;
                            }
                            updateMember(this.memberForm).then(res => {
                                this.submitLoading = false;
                                if (res.code === "0000") {
                                    this.$Message.success("修改成功");
                                    this.queryMemberList(1,10);
                                    this.memberModalVisible = false;
                                }
                            }).catch(() => {
                                this.submitLoading = false;
                            });
                        }else{
                            this.submitLoading = true;
                            batchUpdateMember(this.ids,this.memberForm).then(res => {
                                this.submitLoading = false;
                                if (res.code === "0000") {
                                    this.$Message.success("修改成功");
                                    this.queryMemberList(1,10);
                                    this.memberModalVisible = false;
                                    this.selectCount = 0;
                                }
                            })
                        }
                    }
                })
            },
            //删除
            del(v){
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除成员" + v.userName + " ?",
                    onOk: () => {
                        deleteMember(v).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.queryMemberList(1,10);
                            }
                        });
                    }
                });
            },
            //修改
            update(v){
                this.modalType = 0;
                this.modalTitle = "编辑成员信息";
                // 转换null为""
                for (let attr in v) {
                    if (v[attr] === null) {
                        v[attr] = "";
                    }
                }
                let str = JSON.stringify(v);
                let member = JSON.parse(str);
                this.memberForm = member;
                this.memberForm.totalWeight = v.totalWeight;
                this.memberForm.userRealName = v.userName;
                this.memberForm.grabWeight = v.grabWeight;
                this.memberForm.weightUpper = v.weightUpper;
                this.memberForm.agent = v.agentId+','+v.agent;
                this.memberForm.groupLeader = v.groupLeaderId+','+v.groupLeader;
                this.memberForm.agentGroupLeader = v.agentGroupLeaderId+','+v.agentGroupLeader;
                this.temporary = str;
                this.memberModalVisible = true;
                this.$refs.memberForm.resetFields();
            },
            //批量修改
            modify(){
                if (this.selectCount <= 0) {
                    this.$Message.warning("请至少勾选一项数据");
                    return;
                }
                this.getIds();
                this.modalType = 1;
                this.modalTitle = "编辑成员信息";
                this.$refs.memberForm.resetFields();
                this.memberForm.weight = "";
                this.memberModalVisible = true;
            },
            //代办任务释放
            release(){
                if (this.selectCount !== 1) {
                    this.$Message.warning("请勾选一项数据");
                    return;
                }
                agencyTask(this.selectList).then(res=>{
                    if(res.code === "0000"){
                        this.$Message.success("操作成功");
                        this.getAll();
                        this.queryMemberList(1,10);
                        this.selectCount = 0;
                    }
                })
            },
            //转组
            group(){
                if(this.selectCount===0){
                    this.$Message.warning("请至少选择一项数据");
                    return;
                }
                this.show2 = !this.show2;
                this.groupTransferNotify();
            },
            groupTransferNotify: function () {
                this.$refs.groupTransfer.groupTransferMsg(this.selectList)
            },
            //上岗/离岗
            post(){
                if(this.selectCount===0){
                    this.$Message.warning("请至少选择一项数据");
                    return;
                }
                for (let i = 0; i < this.selectList.length; i++) {
                    if(this.selectList[i].postStatus === "1"){
                        this.$Message.warning("请选择离岗成员");
                        return;
                    }
                }
                this.memberForm.list = this.selectList;
                this.memberForm.changeRemarks = "";
                updatePost(this.memberForm).then(res=>{
                    if(res.code === "0000"){
                        this.$Message.success("操作成功");
                        this.getAll();
                        this.queryMemberList(1,10);
                        this.selectCount = 0;
                    }
                })
            },
            //离岗
            leavePost(){
                if(this.selectCount===0){
                    this.$Message.warning("请至少选择一项数据");
                    return;
                }
                for (let i = 0; i < this.selectList.length; i++) {
                    if(this.selectList[i].postStatus !== "1"){
                        this.$Message.warning("请选择在岗成员");
                        return;
                    }
                }
                this.changeRemarksTitle = "离岗编辑";
                this.changeRemarksModalVisible = true;
            },
            //离岗返回
            cancelLeavePost(){
                this.changeRemarksModalVisible = false;
            },
            //离岗提交
            updateLeavePost(){
                this.$refs.changeRemarksForm.validate(valid => {
                    if(valid){
                        this.memberForm.list = this.selectList;
                        this.memberForm.changeRemarks = this.changeRemarksForm.changeRemarks;
                        updatePost(this.memberForm).then(res=>{
                            if(res.code === "0000"){
                                this.$Message.success("操作成功");
                                this.getAll();
                                this.queryMemberList(1,10);
                                this.selectCount = 0;
                                this.changeRemarksModalVisible = false;
                                this.changeRemarksForm.changeRemarks = "";
                            }
                        })
                    }
                })
            },
            //上岗/离岗记录
            query(){
                if(this.selectCount!==1){
                    this.$Message.warning("请选择一项数据记录查询");
                    return;
                }
                this.getIds();
                this.show = !this.show;
                this.notify();
            },
            notify: function () {
                this.$refs.child.parentMsg(this.ids)
            },
            //日历调整
            calendar(){
                if(this.selectCount===0){
                    this.$Message.warning("请至少选择一项数据");
                    return;
                }
                this.getIds();
                this.show4 = !this.show4;
                this.calendarNotify();
            },
            calendarNotify: function () {
                this.$refs.calendarChange.calendarAdjustment(this.ids);
            },
            //工作日历
            workCalendar(){
                if(this.selectCount!==1){
                    this.$Message.warning("请选择一项数据");
                    return;
                }
                this.getIds();
                this.show5= !this.show5;
                this.calendarRecordNotify();
            },
            calendarRecordNotify: function () {
                this.$refs.calendarRecord.calendarRecordMsg(this.ids);
            },
            //权限调整
            power(){
                if(this.selectCount!==1){
                    this.$Message.warning("请选择一项数据");
                    return;
                }
                this.show3 = !this.show3;
                this.rightsAdjustNotify();
            },
            rightsAdjustNotify: function () {
                this.$refs.rightsAdjust.rightsAdjustMsg(this.selectList);
            },
            //新增
            add(){
                this.show1 = !this.show1;
                this.memberNotify()
            },
            memberNotify: function () {
                this.$refs.memberChild.memberMsg(this.searchForm.groupId)
            },
            getIds(){
                let ids = [];
                this.selectList.forEach(function (e) {
                    ids += e.loginName + ",";
                });
                ids = ids.substring(0, ids.length - 1);
                this.ids = ids;
            },
            //获取用户全部信息
            getAll(){
                getAllUserData().then(res => {
                    if (res.code === "0000") {
                        this.locationData = res.data;
                    }
                });
            },
            //查询成员
            queryMemberList(pageNumber, pageSize){
                if(this.searchForm.groupId!==""){
                    this.searchForm.pageNumber = pageNumber;
                    this.searchForm.pageSize = pageSize;
                    queryMemberList(this.searchForm).then(res => {
                        if(res.code==="0000"){
                            if(res.data.total > 0){
                                res.data.records.forEach(key=>{
                                    let userId = key.loginName;
                                    if(key.weight===0){
                                        key.weight = "0";
                                    }
                                    if(key.agencyNumber===0){
                                        key.agencyNumber = "0";
                                    }
                                    getAllUserData().then(res=> {
                                        if (res.code === "0000") {
                                            res.data.forEach(value=>{
                                                if(userId===value.username){
                                                    this.$set(key,"title",value.departmentTitle);
                                                }
                                            })
                                        }
                                    });
                                });
                            }
                            let {records, total} = res.data;
                            this.$refs.memberInfoTable.updateTableData(records, total);
                        }
                    })
                }
            },
            getUserList(){
                getUserListData(this.searchForm).then(res => {
                    if (res.code === "0000") {
                        let self = this;
                        res.data.records.forEach(key=>{
                            self.$set(key,"userIdName",key.userId+","+key.userRealName);
                        });
                        self.userList = res.data.records;
                    }
                })
            },
            //刷新当前页面
            refreshMember(){
                this.queryMemberList(1,10);
                this.selectCount = 0;
            },
        },
        watch:{
            checkid:function (newValue) {
                if(newValue!==""){
                    this.searchForm.groupId = newValue.id;
                    this.searchForm.groupName = newValue.name;
                    this.memberForm.groupId = newValue.id;
                    this.searchForm.loginName = "";
                    this.searchForm.userName = "";
                    this.queryMemberList(1,10);
                    this.getUserList();
                }
            }
        },
        computed: {
            defines() {
                return [
                    {
                        id: 'memberInfo',
                        fields: memberInfo
                    }
                ]
            }
        },
        //初始化
        mounted() {
            this.init();
        }
    }
</script>
<style>
    .memberManage .ivu-poptip{
        width: auto;
    }
</style>

